define(["jquery","F7","config"],function($,F7,conf){
    // Initialize your app
    var myApp = new Framework7();
    
    // Export selectors engine
    var $$ = Dom7; 
    
    // Add view
    var mainView = myApp.addView('.view-main', {
        // Because we use fixed-through navbar we can enable dynamic navbar
        dynamicNavbar: true
    });
    var _history_pop = function(){
        myApp.getCurrentView().history.pop()
    };
    var _close = function(){
        myApp.closeModal();
    };
    var _error = function(e){
        if(!window.DEV){ 
            console.error("MY_APP THROW ERROR:"+e);
        }
    };
    var _log = function(e){
        if(!window.DEV){ 
            console.log("MY_APP THROW LOG:"+e);
        }
    };
    var _url = function(u){
        return conf.SITE+"/"+u;
    }
    var _back = function(){
       mainView.router.back();  
    };
    var _tpl = function(template,context){
        var compiledTemplate = Template7.compile(template);
        return compiledTemplate(context);
    }
    var _request = function(url,data,cb,param){
        $.ajax({
        	async: true,
        	url: url,
        	data: data,
        	dataType: "json", 
        	beforeSend: function () { 
        	},
        	complete: function () { 
        	},
        	success: function (d, e, f) {
        	    cb(d);
        	},
        	error: function (xhr, textStatus, errorThrown) {
        	   
        	}
        });
    };
    var _component = function(param){
        var path = "";
        if(typeof param == "object"){
            path = param.path;
        }else if(typeof param == "string"){
            path = param;
        }
        if(!path){
            _error("App -> _component -> init -> param -> path is not config");
            return;
        }
        require([path],function(res){
            if(res.context){ 
                mainView.router.load({ //加载单独页面page 
                    url:'src/'+path+'.html',//页面的url 
                    context:res.context
                });
            }
            if(res.popup){
                require(["text!"+path+".html"],function(tpl){ 
                    myApp.popup(tpl);
                });
            }
        },function(){
            if(path === conf.default_router){
                _error("not config config.default_router or config error");
                return ;
            }else{ 
                _component(conf.default_router);
            }
        });
    }; 
    
    var _hash = function(key){
        function getHashParameters(){
            var arr = (location.hash || "").replace(/^\#/,'').split("&");
            var params = {};
            for(var i=0; i<arr.length; i++){
                var data = arr[i].split("=");
                if(data.length == 2){
                     params[data[0]] = data[1];
                }
            }
            return params;
        }
        var params = getHashParameters();
        return params[key];
    }
    var _param = function(key){
        /**
         * 获取请求参数列表
         * @returns {{}}
         */
        function getRequestParameters(key){
            var arr = (location.search || "").replace(/^\?/,'').split("&");
            var params = {};
            for(var i=0; i<arr.length; i++){
                var data = arr[i].split("=");
                if(data.length == 2){
                    params[data[0]] = data[1];
                }
            }
            return params;
        }
        var params = getRequestParameters();
        return params[key];
    }
    
    return {
        myApp:myApp, // Framework7
        $$:$$,  // DOM7
        mainView:mainView,
        tpl:_tpl, //Template7
        history_pop:_history_pop,
        close:_close,
        component:_component, // component
        url:_url,
        back:_back,
        request:_request,
        log:_log,
        error:_error,
        hash:_hash,
        param:_param,
    };

});